const { getTemplateParam } = require('../models/build')

const path = require('path')
const moment = require('moment')
const aliOss = require('./ali-oss')

const lastVersion = async (query) => {
  let aliRet = await aliOss.listInner(query)
  if (!aliRet) {
    return 'v0.0.0'
  }
  let objects = aliRet.objects || []
  let tmp = objects.filter(item => {
    return path.extname(item.name) !== '.zip'
  })
  tmp.sort((a, b) => {
    return moment(b.lastModified) - moment(a.lastModified)
  })
  if (tmp.length == 0) {
    return 'v0.0.0'
  }
  const last = tmp[0].name
  const basename = path.basename(last, path.extname(last))
  re = /v\d+\.\d+(\.\d+)?/i;
  const vs = basename.match(re)
  if ((vs === null) || (vs.length == 0)) {
    return 'v0.0.0'
  }
  return vs[0]
}

const versionSeg = (version) => {
  if ((version === null) || (version === undefined) || (version === '')) {
    return [0, 0, 0]
  }
  if (typeof version !== 'string') {
    return [0, 0, 0]
  }

  const vs = version.slice(1).split('.').map(function (v) { return parseInt(v) })
  if (vs.length === 0) {
    return [0, 0, 0]
  }
  if (vs.length === 1) {
    return [vs[0], 0, 0]
  }
  if (vs.length === 2) {
    return [vs[0], vs[1], 0]
  }
  return [vs[0], vs[1], vs[2]]
}

const build = async (ctx) => {
  let key = ctx.params.key
  let tplParam = await getTemplateParam(key)
  const dev = {
    version: await lastVersion({ prefix: `${key}/dev` })
  }
  const turndev = {
    version: await lastVersion({ prefix: `${key}/turndev` })
  } 
  const test = {
    version: await lastVersion({ prefix: `${key}/test` })
  }
  const prod = {
    version: await lastVersion({ prefix: `${key}/prod` })
  } 
  const baolv = {
    version: await lastVersion({ prefix: `${key}/baolv` })
  }
  const baolvtest = {
    version: await lastVersion({ prefix: `${key}/baolvtest` })
  }
  const refactor = {
    version: await lastVersion({ prefix: `${key}/refactor` })
  }
  const baolvrefactor = {
    version: await lastVersion({ prefix: `${key}/baolvrefactor` })
  }
  const baolvnewprod = {
    version: await lastVersion({ prefix: `${key}/baolvnewprod` })
  }
  const baolvnewtest = {
    version: await lastVersion({ prefix: `${key}/baolvnewtest` })
  }
  const last = {
    dev: { version: dev.version, se: versionSeg(dev.version) },
    turndev: { version: turndev.version, se: versionSeg(turndev.version)  },
    test: { version: test.version, se: versionSeg(test.version)  },
    prod: { version: prod.version, se: versionSeg(prod.version)  },
    baolv: { version: prod.version, se: versionSeg(baolv.version)  },
    baolvtest: { version: baolvtest.version, se: versionSeg(baolvtest.version) },
    refactor: { version: refactor.version, se: versionSeg(refactor.version) },
    baolvrefactor: { version: baolvrefactor.version, se: versionSeg(baolvrefactor.version) },
    baolvnewprod: { version: baolvnewprod.version, se: versionSeg(baolvnewprod.version) },
    baolvnewtest: { version: baolvnewtest.version, se: versionSeg(baolvnewtest.version) },
  }
  tplParam = { ...tplParam,  last }
  await ctx.render('build', tplParam)
}

module.exports = {
  build
}
